home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3.iso / chapte11 / virtaloc.c < prev    next >
C/C++ Source or Header  |  1996-01-29  |  8KB  |  227 lines

  1.  
  2. #include <windows.h>  
  3. #include "VirtAloc.h" 
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17.  
  18. LPCTSTR lpszAppName  = "MyApp";
  19. LPCTSTR lpszTitle    = "My Application"; 
  20.  
  21. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  22.  
  23. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  24.                       LPTSTR lpCmdLine, int nCmdShow)
  25. {
  26.    MSG      msg;
  27.    HWND     hWnd; 
  28.    WNDCLASS wc;
  29.  
  30.    // Register the main application window class.
  31.    //............................................
  32.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  33.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  34.    wc.cbClsExtra    = 0;                      
  35.    wc.cbWndExtra    = 0;                      
  36.    wc.hInstance     = hInstance;              
  37.    wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
  38.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  39.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  40.    wc.lpszMenuName  = lpszAppName;              
  41.    wc.lpszClassName = lpszAppName;              
  42.  
  43.    if ( IS_WIN95 )
  44.    {
  45.       if ( !RegisterWin95( &wc ) )
  46.          return( FALSE );
  47.    }
  48.    else if ( !RegisterClass( &wc ) )
  49.       return( FALSE );
  50.  
  51.    hInst = hInstance; 
  52.  
  53.    // Create the main application window.
  54.    //....................................
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  108. {
  109. static LPVOID lpMem = NULL;
  110.  
  111.    switch( uMsg )
  112.    {
  113.       case WM_CREATE  : 
  114.               // Reserve 1MB of virtual memory.
  115.               //...............................
  116.               lpMem = VirtualAlloc( NULL, 1048576, MEM_RESERVE, 0 );
  117.               break;
  118.  
  119.       case WM_COMMAND :
  120.               switch( LOWORD( wParam ) )
  121.               {
  122.                  case IDM_TEST :
  123.                         {
  124.                            // Commit a 70KB block of memory from virtual
  125.                            // memory then use it.
  126.                            //...........................................
  127.                            DWORD  dwoldAccess;
  128.                            LPTSTR lpBuf = VirtualAlloc( lpMem, 71680,
  129.                                                   MEM_COMMIT, PAGE_READWRITE );
  130.                            
  131.                            // Set each 1KB block of memory to a number.
  132.                            //..........................................
  133.                            if ( lpBuf )
  134.                            {
  135.                               int i;
  136.                               for( i = 0; i < 70; i++ )
  137.                               {
  138.                                  if ( VirtualLock( (lpBuf+(i*1024)), 1024 ) )
  139.                                  {
  140.                                     memset( (lpBuf+(i*1024)), i, 1024 );
  141.                                     VirtualUnlock( (lpBuf+(i*1024)), 1024 );
  142.                                  } 
  143.                               } 
  144.                            }
  145.  
  146.                            // Protect the memory with Read-Only access.
  147.                            //..........................................
  148.                            VirtualProtect( lpBuf, 71680, PAGE_READONLY,
  149.                                            &dwoldAccess );
  150.  
  151.                            __try // to read from the memory.
  152.                            {
  153.                               char szMsg[20];
  154.  
  155.                               wsprintf( szMsg, "Memory Value = %d",
  156.                                         *(lpBuf+2048) );
  157.                               MessageBox( hWnd, szMsg, "Read", 
  158.                                           MB_OK | MB_ICONINFORMATION );
  159.                            }
  160.                            __except( 1 )
  161.                            {
  162.                               MessageBox( hWnd, "Error accessing memory",
  163.                                          "Read", 
  164.                                           MB_OK | MB_ICONEXCLAMATION );
  165.                            }
  166.  
  167.                            __try  // to write to the memory.
  168.                            {
  169.                               *lpBuf = 10;
  170.                            }
  171.                            __except( 1 )
  172.                            {
  173.                               MessageBox( hWnd, "Error accessing memory", 
  174.                                          "Write", 
  175.                                           MB_OK | MB_ICONEXCLAMATION );
  176.                            }
  177.                         }
  178.                         break;
  179.  
  180.                  case IDM_ABOUT :
  181.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  182.                         break;
  183.  
  184.                  case IDM_EXIT :
  185.                         DestroyWindow( hWnd );
  186.                         break;
  187.               }
  188.               break;
  189.       
  190.       case WM_DESTROY :
  191.               // Free virtual memory.
  192.               //.....................
  193.               VirtualFree( lpMem, 0, MEM_RELEASE );  
  194.               PostQuitMessage(0);
  195.               break;
  196.  
  197.       default :
  198.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  199.    }
  200.  
  201.    return( 0L );
  202. }
  203.  
  204.  
  205. LRESULT CALLBACK About( HWND hDlg,           
  206.                         UINT message,        
  207.                         WPARAM wParam,       
  208.                         LPARAM lParam)
  209. {
  210.    switch (message) 
  211.    {
  212.        case WM_INITDIALOG: 
  213.                return (TRUE);
  214.  
  215.        case WM_COMMAND:                              
  216.                if (   LOWORD(wParam) == IDOK         
  217.                    || LOWORD(wParam) == IDCANCEL)    
  218.                {
  219.                        EndDialog(hDlg, TRUE);        
  220.                        return (TRUE);
  221.                }
  222.                break;
  223.    }
  224.  
  225.    return (FALSE); 
  226. }
  227.